package top.huhuiyu.springboot.study.base;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 带数据的应答结果
 *
 * @author 胡辉煜
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class BaseDataResult<T> extends BaseResult {
  private static final long serialVersionUID = 1L;
  private T data;

  /**
   * 创建成功的应答结果
   *
   * @param data 应答数据
   * @return 应答结果
   */
  public static <T> BaseDataResult<T> ok(T data) {
    BaseDataResult<T> result = new BaseDataResult<>();
    result.setSuccess(true);
    result.setCode(200);
    result.setData(data);
    return result;
  }

  /**
   * 创建失败的应答结果
   *
   * @param message 应答消息
   * @param clazz   应答数据类型
   * @return 应答结果
   */
  public static <T> BaseDataResult<T> fail(String message, Class<T> clazz) {
    BaseDataResult<T> result = new BaseDataResult<>();
    result.setSuccess(false);
    result.setCode(500);
    result.setMessage(message);
    return result;
  }

}
